<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Controllers
 * @see 		Orders
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Orders extends CI_Controller {
	var $status = '';
	var $q = '';
	var $limit = '';
	var $time_from = '';
	var $time_to = '';
	var $orderby = '';
	var $ordermode = '';
	var $updated = '';
	var $failed = '';
	var $name = '';
	var $uri_arr = array();
	var $uri_str = '';
	var $site_url = '';

	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (check_rights() == false)
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'Ord');
		$this->status = $GLOBALS['var']['filter_cat'];
		$this->q = $this->input->get('q', true);
		$this->rowstart = $this->input->get('rowstart', true);
		$this->time_from = $this->input->get('from', true);
		$this->time_to = $this->input->get('to', true);
		$this->orderby = $this->input->get('orderby', true);
		$this->ordermode = $this->input->get('ordermode', true);
		$this->updated = $this->input->get('updated', true);
		$this->failed = $this->input->get('failed', true);
		$this->name = $this->input->get('name', true);
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'],
			'status' => $this->status,
			'q' => $this->q,
			'limit' => $this->limit,
			'from' => $this->time_from, 
			'to' => $this->time_to,
			'rowstart' => $GLOBALS['var']['rowstart'],
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode
		);
		$this->uri_str = url_uri($this->uri_arr);
		$this->site_url = site_url($GLOBALS['var']['act']);
	}

	public function index()
	{
		/*
		* Xu ly du lieu
		*/
		$num_rows = $this->Ord->show($this->uri_arr, true);
		$data = array(
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode,
			'site_url' => $this->site_url,
			'uri_str' => $this->uri_str,
			'orders' => array()
		);
		$rows = $this->Ord->show($this->uri_arr);
		if ($rows) $data['orders'] = $rows;
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Hóa đơn bán hàng',
			'add_link' => '',
			'search' => true,
			'page_list' => page_list($num_rows, $this->uri_arr),
			'datetime_picker' => false,
			'submit_btn' => false,
			'cat_list' => $this->Ord->filter_status_list(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/index', $data);
		$this->load->view('footer');
	}

	public function update($id = '')
	{
		/*
		* Truyen thuoc tinh
		*/
		$methods =& get_instance();
		/*
		* Xu ly du lieu
		*/
		$status_arr = array();
		foreach ($GLOBALS['orders_status'] as $status) {
			$status_arr[] = $status;
		}
		$info = $this->Ord->info($id);
		$data = array(
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'action' => site_url($GLOBALS['var']['act'].'/process').$this->uri_str,
			'info' => $info,
			'methods' => $methods,
			'status_arr' => $status_arr
		);
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Chi tiết đơn hàng',
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => true,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/update', $data);
		$this->load->view('footer');
	}

	public function product_list($ma)
	{
		if (!$ma)
		{
			return false;
		}
		return $this->Ord->product_list($ma);
	}

	public function process()
	{
		/*
		* Kiem tra quyen cap nhap
		*/
		if (!$GLOBALS['per']['edit'])
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Kiem tra POST method
		*/
		if(!$_POST)
		{
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$id = $this->input->post('id', true);
		$status = $this->input->post('status', true);
		if (!token_validation() || !$status || !$id)
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Them tham so url
		*/
		$ma = $this->input->post('ma', true);
		$this->uri_arr['name'] = str_replace('&', '', $ma);
		/*
		* Xu ly du lieu
		*/
		$data = array(
			'status' => $status,
			'ma' => $ma,
			'date_modified' => date(TIME_SQL)
		);
		/*
		* Ghi du lieu
		*/
		if ($this->Ord->process($data, $id))
		{
			$this->uri_arr['updated'] = 1;
		}
		else
		{
			$this->uri_arr['failed'] = 1;
		}
		/*
		* Chuyen huong
		*/
		redirect($GLOBALS['var']['act'].'/update/'.$id.'/'.url_uri($this->uri_arr));
	}

	public function printer($id = '')
	{
		/*
		* Kiem tra ID
		*/
		if (!$id)
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Truyen thuoc tinh
		*/
		$methods =& get_instance();
		/*
		* Xu ly du lieu
		*/
		$info = $this->Ord->info($id);
		$data = array(
			'info' => $info,
			'methods' => $methods
		);
		/*
		* Hien thi
		*/
		$this->load->view($GLOBALS['var']['act'].'/printer', $data);
	}
}

/* End of file orders.php */
/* Location: ./application/controllers/orders.php */